package com.honyelchak.shortlink.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.honyelchak.shortlink.base.common.convention.result.Result;
import com.honyelchak.shortlink.base.common.convention.result.Results;
import com.honyelchak.shortlink.project.dto.req.ShortLinkCreateReqDTO;
import com.honyelchak.shortlink.project.dto.req.ShortLinkPageReqDTO;
import com.honyelchak.shortlink.project.dto.req.ShortLinkUpdateReqDTO;
import com.honyelchak.shortlink.project.dto.resp.ShortLinkCountQueryRespDTO;
import com.honyelchak.shortlink.project.dto.resp.ShortLinkCreateRespDTO;
import com.honyelchak.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.honyelchak.shortlink.project.service.ShortLinkService;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class ShortLinkController {

    private final ShortLinkService shortLinkService;


    /**
     * 创建短链接
     */
    @PostMapping("/api/short-link/v1/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO reqDTO) {
        return Results.success(shortLinkService.createShortLink(reqDTO));
    }

    /**
     * 更新短链接
     */
    @PutMapping("/api/short-link/v1/update")
    public Result<Void> createShortLink(@RequestBody ShortLinkUpdateReqDTO reqDTO) {
        shortLinkService.updateShortLink(reqDTO);
        return Results.success();
    }

    /**
     * 短链接跳转
     */
    @GetMapping("/{short-uri}")
    public void restoreUrl(@PathVariable(name = "short-uri") String shortUri, ServletRequest request, ServletResponse response){
        shortLinkService.restoreUrl(shortUri, request, response);
    }

    /**
     * 短链接分页
     */
    @GetMapping("/api/short-link/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO reqDTO){
        return Results.success(shortLinkService.pageShortLink(reqDTO));
    }

    /**
     * 短链接计数
     */
    @GetMapping("/api/short-link/v1/count")
    public Result<List<ShortLinkCountQueryRespDTO>> listGroupShortLinkCount(@RequestParam List<String> requestParam){
        return Results.success(shortLinkService.listGroupShortLinkCount(requestParam));
    }

}
